/**
 * 
 */
package com.huarui.user.security;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.StrutsStatics;
import org.ewaf.framework.cookie.CookieManager;

import com.huarui.user.action.LoginAction;
import com.huarui.user.action.RegisterAction;
import com.huarui.user.model.CookieConstants;
import com.huarui.user.model.SessionMessage;
import com.huarui.user.model.User;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

/**
 * @author peng.sunp
 * 
 */
public class ThreadlocalInterceptor implements Interceptor {
	private static final long serialVersionUID = 7599544896612375980L;
	private Logger logger = Logger.getLogger(ThreadlocalInterceptor.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.interceptor.Interceptor#destroy()
	 */
	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.interceptor.Interceptor#init()
	 */
	@Override
	public void init() {
		
		// TODO Auto-generated method stub

	}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		HttpServletRequest request = (HttpServletRequest) invocation
			.getInvocationContext().get(StrutsStatics.HTTP_REQUEST);
		
		try {
			CookieManager cookie = new CookieManager(request);
		
			SessionMessage sessionMessage = UserCookieAuther.getSessionMessage(cookie);
			UserHolder.setCurrenSessionMessage(sessionMessage);		
			return invocation.invoke();
		} finally {
			UserHolder.clear();
		}
	}
	
}
